// todo 用户模块的vuex操作

import { getStorageToken, removeStorageToken, setStorageToken } from '@/utils/storage'
import { getUserInfo } from '@/api/user'
// import { Toast } from 'vant'

export default {
  namespaced: true,
  state: {
    token_obj: getStorageToken(),
    userInfo: {}
  },
  mutations: {
    // 设置token
    setToken(state, payload) {
      state.token_obj = payload
      setStorageToken(payload)
    },
    // 删除token
    removeToken(state) {
      state.token_obj = []
      // 清楚本地存储的token
      removeStorageToken()
    },
    // 设置用户信息
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    // 异步操作
    async getUserInfoAsync(context) {
      try {
        const { data: res } = await getUserInfo()
        context.commit('setUserInfo', res)
      } catch (e) {
        // Toast('获取用户信息失败')
      }
    }
  },
  getters: {}
}
